/**
 *    TimedProfiles - let Android perform tasks based on conditions
 *    Copyright (C) 2011  Daniel Weinhold
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * */
package de.drak.Profiles;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

/**Simple WebView for Online-Help*/
public class OnlineHelp extends Activity implements View.OnKeyListener {
	/**Key holding the URI to display*/	public static final String HELP_URL = "com.twofortyfouram.locale.intent.extra.HELP_URL";
	/**URI where Help is located*/		public static final String HELP_URL_PRE   = "http://android.drak.li/TP/help/";
	private final int MENU_BACK = 0;
	private final int MENU_CLOSE = 1;
	WebView webview;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        webview = new WebView(this);
		getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(webview);
        
        webview.setOnKeyListener(this);
        
        //load title
		TimedProfiles.createTitle(this, getString(R.string.onlinehelp));

		webview.getSettings().setJavaScriptEnabled(true);

		 final Activity activity = this;
		 webview.setWebChromeClient(new WebChromeClient() {
		   public void onProgressChanged(WebView view, int progress) {
		     // Activities and WebViews measure progress with different scales.
		     // The progress meter will automatically disappear when we reach 100%
		     activity.setProgress(progress * 100);
		   }
		 });
		 webview.setWebViewClient(new WebViewClient() {
			   public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
			     Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
			   }
			   public boolean shouldOverrideUrlLoading(WebView view, String url)
			   {
				   if(url.startsWith("http://"))
				   {
					   view.loadUrl(url);
					   return true;
				   }
				return false;
			   }
			 });

		
        //load webadress
		final String url = getIntent().getStringExtra(HELP_URL);
		if (url == null)
		{
	        String summary = "<html><body>You ordered <b>Help</b>, but no related URL was given<br/>"
	        	+"<a href=\"http://android.drak.li/TP\">Timed Profiles Main Page</a><br/>"
	        	+"<a href=\"http://android.drak.li\">Homepage</a>"
	        	+"</body></html>";
	        webview.loadData(summary, "text/html", "utf-8");
		}else
		{
			webview.loadUrl(url);
		}
    }
	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		if(event.getAction()==KeyEvent.ACTION_DOWN && keyCode==KeyEvent.KEYCODE_BACK)
		{
			if(((WebView)v).canGoBack())
			{
				//user wishes to go back in history or wants to leave online help?
				openOptionsMenu();
				return true;
			}
		}
		return false;
	}
	@Override
	public boolean onCreateOptionsMenu(final Menu menu)
	{
		super.onCreateOptionsMenu(menu);

		menu.add(0, MENU_BACK, 0, R.string.help_back)
			.setIcon(R.drawable.help_back).getItemId();
		menu.add(0, MENU_CLOSE, 0, R.string.men_exit)
			.setIcon(android.R.drawable.ic_menu_close_clear_cancel).getItemId();
		return true;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean onMenuItemSelected(final int featureId, final MenuItem item)
	{
		switch (item.getItemId())
		{
			case MENU_CLOSE:
			{
				finish();
				return true;
			}
			case MENU_BACK:
			{
				webview.goBack();
				return true;
			}
		}

		return super.onOptionsItemSelected(item);
	}
}
